
#include "freetype_dir.h"
/*总结
1 首先创建一个库
2 根据字体文件得到face object
3 设置字体大小及第一个坐标值 
4 加载face得到字体的glyph及buffer
5 显示 （每次显示一个字符都是根据前一个字符的glyph得到下一个字符坐标的原点值，这样方便显示）
*/

static FT_Library g_tLibrary;
static FT_Face g_tFace;
static FT_GlyphSlot g_tSlot;
static unsigned char *filename=NULL;
 


Font_Dir_t freetype = {
	.name = "Freetype" ,
	.Font_Dir_init = current_Font_Dir_init,
	.Font_Dir_Set_Size = current_Font_Dir_Set_Size,
	.Font_Dir_Get_Bitmap = current_Font_Dir_Get_Bitmap,
	  
};

int current_Font_Dir_init(char* pcFontFile) {

	int iError;
	/* 显示矢量字体 */
	iError = FT_Init_FreeType(&g_tLibrary);			   /* 初始化库 */
    iError = FT_New_Face(g_tLibrary, pcFontFile, 0, &g_tFace); /*根据字体文件得到face */
	g_tSlot = g_tFace->glyph;
	iError = FT_Set_Pixel_Sizes(g_tFace, 50, 0); /* 默认字体大小50*/
	return 0;
}

int current_Font_Dir_Set_Size(unsigned int dwFontSize) {
	FT_Set_Pixel_Sizes(g_tFace, dwFontSize, 0);
	return 0;
}

int current_Font_Dir_Get_Bitmap(unsigned int dwCode,pFontBitMap_t current_BitMap) {
	int iError;
	int iPenX = current_BitMap->iCurOriginX;
	int iPenY = current_BitMap->iCurOriginY;
 
	/* load glyph image into the slot (erase previous one) */
	//iError = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER );
	iError = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
 
	//DBG_PRINTF("iPenX = %d, iPenY = %d, bitmap_left = %d, bitmap_top = %d, width = %d, rows = %d\n", iPenX, iPenY, g_tSlot->bitmap_left, g_tSlot->bitmap_top, g_tSlot->bitmap.width, g_tSlot->bitmap.rows);
	current_BitMap->iXLeft    = iPenX + g_tSlot->bitmap_left;
	current_BitMap->iYTop     = iPenY - g_tSlot->bitmap_top;
	current_BitMap->iXMax     = current_BitMap->iXLeft + g_tSlot->bitmap.width;
	current_BitMap->iYMax     = current_BitMap->iYTop  + g_tSlot->bitmap.rows;
	current_BitMap->iBpp      = 1;  //?这里的设置 ibpp位多少
 	current_BitMap->iPitch    = g_tSlot->bitmap.pitch;
	current_BitMap->pucBuffer = g_tSlot->bitmap.buffer;
	
	current_BitMap->iNextOriginX = iPenX + g_tSlot->advance.x / 64;
	current_BitMap->iNextOriginY = iPenY;
 
	return 0;

}





